﻿using CopyBaron.ProcessList;
using CopyBaron.ProcessList.WindowActivator;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Input;

namespace CopyBaron.Model
{
    public class CopyBaron2
    {
        private CopyBaronSettings _settings;
        private string _clipboardText;

        public CopyBaron2(CopyBaronSettings settings)
        {
            _settings = settings;
        }

        public void CleanClipboard()
        {
            if (!Clipboard.ContainsText())
                return;

            _clipboardText = Clipboard.GetText();

            trimText();
            removeLineBreaks();
            removeDoubleWhitespaces();
            replaceTabs();
            Clipboard.SetText(_clipboardText);

            var previousWindowIsActivated = activatePreviousWindow();
            sendCtrlV(previousWindowIsActivated);

            playSound();
        }

        private void trimText()
        {
            if (_settings.TrimText)
            {
                _clipboardText = _clipboardText.Trim();
            }
        }

        private void removeLineBreaks()
        {
            if (_settings.RemoveLineBreaks)
            {
                _clipboardText = _clipboardText.Replace("\r\n", " "); // Windows
                _clipboardText = _clipboardText.Replace("\n", " "); // AmigaOS, Linux, Mac OS >= 10...
                _clipboardText = _clipboardText.Replace("\r", " "); // Mac OS < 10
            }
        }

        private void removeDoubleWhitespaces()
        {
            if (_settings.RemoveDoubleWhitespaces)
            {
                while (_clipboardText.IndexOf("  ") >= 0)
                {
                    _clipboardText = _clipboardText.Replace("  ", " ");
                }
            }
        }

        private void replaceTabs()
        {
            if (_settings.ReplaceTabs)
            {
                _clipboardText = _clipboardText.Replace("\t", _settings.TabReplacement);
            }
        }

        private bool activatePreviousWindow()
        {
            var previousWindowIsActivated = false;
            if (_settings.ActivatePreviousApplication)
            {
                previousWindowIsActivated = WindowActivator.ActivatePreviousWindow();
            }
            return previousWindowIsActivated;
        }

        private void sendCtrlV(bool previousWindowIsActivated)
        {
            if (_settings.SendCtrlV && previousWindowIsActivated && !System.Diagnostics.Debugger.IsAttached)
            {
                // Ctrl+V nicht senden, wenn das Programm aus VS gestartet wurde, da das
                // ziemlich nervt, wenn im Quellcode die Zwischenablage eingefügt wird. ;-)

                System.Windows.Forms.SendKeys.SendWait("^{v}");
            }
        }

        private void playSound()
        {
            if (_settings.PlaySound)
            {
                System.Media.SystemSounds.Beep.Play();
            }
        }
    }
}
